<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1000">
    <el-form
      ref="formRef"
      :model="productData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
    <el-row :gutter="20">
    <el-col :span="6">
      <el-form-item label="带量总数" prop="quantityCount">
        <el-input v-model="formData.quantityCount" placeholder="请输入带量总数" />
      </el-form-item>
    </el-col>
    <el-col :span="8">
      <el-form-item label="任务起始时间" prop="taskStartTime">
        <el-date-picker
          v-model="formData.taskStartTime"
          type="date"
          value-format="x"
          placeholder="选择带量任务起始时间"
        />
      </el-form-item>
    </el-col>
    <el-col :span="8">
      <el-form-item label="任务截止时间" prop="taskEndTime">
        <el-date-picker
          v-model="formData.taskEndTime"
          type="date"
          value-format="x"
          placeholder="选择带量任务截止时间"
        />
      </el-form-item>
    </el-col>
    </el-row>
     <!--  <el-form-item label="产品条码" prop="barCode">
        <el-input v-model="formData.barCode" placeholder="请输入产品条码" />
      </el-form-item>
      <el-form-item label="产品分类编号" prop="categoryId">
        <el-input v-model="formData.categoryId" placeholder="请输入产品分类编号" />
      </el-form-item>
      <el-form-item label="单位编号" prop="unitId">
        <el-input v-model="formData.unitId" placeholder="请输入单位编号" />
      </el-form-item>
      <el-form-item label="产品规格" prop="standard">
        <el-input v-model="formData.standard" placeholder="请输入产品规格" />
      </el-form-item>
      <el-form-item label="科室编号" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入科室编号" />
      </el-form-item>
      <el-form-item label="带量数量" prop="count">
        <el-input v-model="formData.count" placeholder="请输入带量数量" />
      </el-form-item> -->
    </el-form>
    <!-- 子表的表单 -->
    <el-tabs v-model="subTabsName">
      <el-tab-pane label="科室" name="dept">
        <!-- <DeptForm ref="deptFormRef" :id="formData.productId" /> -->
        <DeptForm ref="deptFormRef" :id="formData.productId" :quantityCount="formData.quantityCount" />
      </el-tab-pane>
    </el-tabs>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DepartQuantityApi, DepartQuantityVO } from '@/api/spd/manager/departquantity'
import DeptForm from './components/DeptForm.vue'
import { ProductApi, ProductVO } from '@/api/erp/product/product'
/** 带量分配 表单 */
defineOptions({ name: 'DepartQuantityForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const productData = ref({
  id: undefined,
  name: undefined,
  barCode: undefined,
  categoryId: undefined,
  unitId: undefined,
  status: undefined,
  standard: undefined,
  // batch:undefined,
  remark: undefined,
  // validityDate: undefined,
  weight: undefined,
  purchasePrice: undefined,
  salePrice: undefined,
  isQuantity:undefined,
  quantityCount:undefined,
  year:undefined,
  taskStartTime:undefined,
  taskEndTime:undefined,
  minPrice: undefined
})
const formData = ref({
  id: undefined,
  productId: undefined,
  productName: undefined,
  barCode: undefined,
  categoryId: undefined,
  unitId: undefined,
  standard: undefined,
  deptId: undefined,
  count: undefined,
  depts:undefined,
  quantityCount:undefined,
  year:undefined,
  taskStartTime:undefined,
  taskEndTime:undefined,
})
const formRules = reactive({

})
const formRef = ref() // 表单 Ref

/** 子表的表单 */
const subTabsName = ref('dept')
const deptFormRef = ref('id')

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  console.log("接受传递上级页面传值1",id)
  dialogVisible.value = true
  dialogTitle.value = t('分配')
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true

    try {
      productData.value = await ProductApi.getQuantity(id)
      console.log("2222",productData.value)
      formData.value.productId = productData.value.id
      formData.value.quantityCount= productData.value.quantityCount
      formData.value.taskStartTime = productData.value.taskStartTime
      formData.value.taskEndTime = productData.value.taskEndTime
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  if(formData.value.taskStartTime==null){
    message.error('任务起始时间不能为空');
    return false;
  }
  if (formData.value.taskStartTime && formData.value.taskEndTime) {
        const startTime = new Date(formData.value.taskStartTime);
        const endTime = new Date(formData.value.taskEndTime);
        if (startTime > endTime) {
          message.error('任务截止时间不能早于任务起始时间');
          return false; // 或者可以重置endTime为空等操作，根据需求来定
    }
  }
  // 校验表单
  await formRef.value.validate()
  // 校验子表单
  try {
  } catch (e) {
    subTabsName.value = 'dept'
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    formData.value.productId = productData.value.id
    formData.value.productName = productData.value.name
    formData.value.barCode = productData.value.barCode
    formData.value.categoryId = productData.value.categoryId
    formData.value.unitId = productData.value.unitId
    formData.value.standard = productData.value.standard
    formData.value.depts = deptFormRef.value.getData()
    console.log("1111",formData.value.depts)

    const data = formData.value as unknown as DepartQuantityVO
    // 拼接子表的数据
    await DepartQuantityApi.createDepartQuantity(data)
    message.success(t('common.allocationSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    productId: undefined,
    productName: undefined,
    barCode: undefined,
    categoryId: undefined,
    unitId: undefined,
    standard: undefined,
    deptId: undefined,
    count: undefined,
    depts:undefined,
    quantityCount:undefined,
    year:undefined,
    taskStartTime:undefined,
    taskEndTime:undefined,
  }
  formRef.value?.resetFields()
}
</script>